package com.proton.ecgpaste;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.proton.ecgcard.algorithm.bean.AlgorithmResult;
import com.proton.ecgcard.algorithm.callback.AlgorithmResultListener;
import com.proton.ecgpaste.connector.EcgPasteManager;
import com.proton.ecgpaste.connector.callback.DataListener;
import com.proton.view.EcgRealTimeView;
import com.wms.ble.bean.ScanResult;
import com.wms.ble.callback.OnConnectListener;
import com.wms.ble.callback.OnScanListener;
import com.wms.ble.utils.BluetoothUtils;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private EcgRealTimeView ecgView;
    private TextView heartRate;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PermissionUtils.getLocationPermission(this);
        PermissionUtils.getReadAndWritePermission(this);

        ecgView = findViewById(R.id.id_ecg_view);
        heartRate = findViewById(R.id.id_heart_rate);

        //todo 搜索设备需要定位权限，android6.0及其以上需要动态申请权限
        //todo 或者手机权限管理里面直接允许定位权限
        final EcgPasteManager manager = EcgPasteManager.getInstance("C3:37:39:AB:69:BC");
        if (!BluetoothUtils.isBluetoothOpened()) {
            BluetoothUtils.openBluetooth();
        }
        findViewById(R.id.id_search_device).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo 需要指定扫描时间可以用三个参数的扫描方法,单位秒
                EcgPasteManager.scanDevice(new OnScanListener() {

                    @Override
                    public void onScanStart() {
                        Log.e(TAG, "onScanStart...");
                    }

                    @Override
                    public void onDeviceFound(ScanResult scanResult) {
                        Log.w(TAG, "onDeviceFound: " + scanResult.getDevice().getAddress());
                    }

                    @Override
                    public void onScanStopped() {
                        Log.e(TAG, "onScanStopped...");
                    }

                    @Override
                    public void onScanCanceled() {
                        Log.e(TAG, "onScanCanceled...");
                    }
                });
            }
        });

        findViewById(R.id.id_start_measure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.setDataListener(new DataListener() {
                    @Override
                    public void receiveEcgFilterData(List<Float> ecgDatas) {
                        Log.w(TAG, "接收心电数据用来绘制实时心电图:" + ecgDatas.size());
                        ecgView.addEcgData(ecgDatas);
                        if (!ecgView.isRunning()) {
                            ecgView.startDrawWave();
                        }
                    }

                    @Override
                    public void receiveBattery(Integer battery) {
                        Log.w(TAG, "receiveBattery:" + battery);
                    }

                    @Override
                    public void receiverHeartRate(int rate) {
                        Log.w(TAG, "心率:" + rate);
                        heartRate.setText(rate + " BPM");
                    }

                    @Override
                    public void receiveEcgSourceData(List<Float> sourceData) {
                        Log.w(TAG, "源数据:" + sourceData.size());
                    }

                    @Override
                    public void signalInterference(int signalQualityIndex) {
                        Log.w(TAG, "信号质量:" + signalQualityIndex);
                    }

                    @Override
                    public void receiveDataValidate(boolean isValidate) {
                        Log.w(TAG, "数据有效性:" + isValidate);
                    }

                    @Override
                    public void receiveFallDown(boolean isFallDown) {
                        Log.w(TAG, "是否跌倒:" + isFallDown);
                    }

                    @Override
                    public void receivePackageNum(int packageNum) {
                        Log.w(TAG, "包序:" + packageNum);
                    }
                });
                //开始连接
                //todo 需要将第一个参数替换为你自己的心电卡设备mac地址
                manager.connectEcgPaste(new OnConnectListener() {
                    @Override
                    public void onConnectSuccess() {
                        Log.w(TAG, "连接成功");
                    }

                    @Override
                    public void onConnectFaild() {
                        Log.w(TAG, "连接失败");
                    }

                    @Override
                    public void onDisconnect() {
                        Log.w(TAG, "连接断开");
                    }
                });
            }
        });

        findViewById(R.id.id_get_result).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo 需要在断开连接之前调用，该方法为异步方法，调用默认参数的则可能体脂率，情绪，等指标不准确，默认参数只是用来获取滤波数据
                //todo 你可以用getAnalysisResult多参数来获取准确的分析结果
                manager.getAnalysisResult(new AlgorithmResultListener() {
                    @Override
                    public void receiveAlgorithmResult(AlgorithmResult algorithmResult) {
                        Log.w(TAG, "分析结果:" + algorithmResult);
                    }
                });
                manager.disConnect();
                ecgView.stopDrawWave();
                ecgView.cleanWave();
            }
        });

        findViewById(R.id.id_end_measure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.disConnect();
                ecgView.stopDrawWave();
                ecgView.cleanWave();
            }
        });
    }
}
